package com.polaris.live.widget

import android.content.Context
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.text.TextPaint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
/**
 * 文本渐变
 *
 * @author Created by 半仙 on 2024/4/17/017 12:00
 */
class GradientTextView(context: Context, attributeSet: AttributeSet) :
    AppCompatTextView(context, attributeSet) {

    private var linearGradient: LinearGradient? = null
    private var paint: Paint? = null

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (w > 0 && h > 0) {
            paint = getPaint()
            // 设置线性渐变，颜色从红色到蓝色
            linearGradient = LinearGradient(
                0f, 0f, w.toFloat(), 0f,
                intArrayOf(
                    ColorUtils.getColor(R.color.color_ff8ab1),
                    ColorUtils.getColor(R.color.color_ffd48b)
                ), null, Shader.TileMode.CLAMP
            )
            (paint as TextPaint).shader = linearGradient
        }
    }


}